//
//  YLRegularFinancingDetailController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/13.
//  Copyright © 2017年 she1323. All rights reserved.
//  定期理财详情

#import "YLRegularFinancingDetailController.h"
#import "YLRegularFinancingHeader.h"
#import "YLGlobeHeader.h"
#import "YLRegularFinancingItemCell.h"
#import "YLTitleHeader.h"
#import "YLRegularFinancingDetailViewModel.h"
#import "YLSubTitileNoArrowTableViewCell.h"
#import "YLCollectionDetailsViewController.h"
#import "YLRegularFinancingDetailModel.h"
@interface YLRegularFinancingDetailController ()


@property (nonatomic, strong) YLRegularFinancingDetailViewModel *viewModel;

@end

@implementation YLRegularFinancingDetailController



- (YLRegularFinancingDetailViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLRegularFinancingDetailViewModel alloc] init];
    }
    return _viewModel;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    YLRegularFinancingHeader *header = [[YLRegularFinancingHeader alloc] init];
    header.frame = CGRectMake(0, 0, YLScreenW, 80);
    self.tableView.tableHeaderView = header;

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLRegularFinancingItemCell class]) bundle:nil] forCellReuseIdentifier:[YLRegularFinancingItemCell cellID]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLSubTitileNoArrowTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLSubTitileNoArrowTableViewCell cellID]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收款详情" style:UIBarButtonItemStylePlain target:self action:@selector(collectionDetails)];
  
    if (_contractCode) {
        
        __weak typeof(self) weakSelf = self;
        [[self.viewModel.requestCommand execute:_contractCode] subscribeNext:^(id  _Nullable x) {
            
            [weakSelf.tableView reloadData];
            weakSelf.title = weakSelf.viewModel.productName;
            header.model = weakSelf.viewModel.model;
        }];
    }
    
    
}

- (void)collectionDetails
{
    YLCollectionDetailsViewController *detail = [[YLCollectionDetailsViewController alloc] init];
    
    if (self.viewModel.model) {
        detail.contractCode = self.viewModel.model.contractCode;
    }
    
    [self.navigationController pushViewController:detail animated:YES];
}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.viewModel) {
        return 2;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return self.viewModel.models.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        YLRegularFinancingItemCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLRegularFinancingItemCell cellID]];
        cell.model = self.viewModel.model;
        return cell;
    }
        YLSubTitileNoArrowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLSubTitileNoArrowTableViewCell cellID]];
        cell.model = self.viewModel.models[indexPath.row];
        return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
       YLTitleHeader *header = [YLTitleHeader new];
        header.title = @"申购详情";
        return header;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 10;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 60;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 78;
    }
    return 50;
}



@end
